/*!
 * \file test_network_utils_comprehensive.cpp
 * \project WonderTrader
 *
 * \author AI Assistant
 * \date 2024/12/01
 * 
 * \brief 网络工具类的综合测试文件
 * 
 * \details 本文件包含对网络处理工具类的全面测试，涵盖：
 *          - URL解析和构建功能
 *          - HTTP请求和响应处理
 *          - 网络连接管理
 *          - 数据编码和解码
 *          - 错误处理和重试机制
 *          - 性能测试和并发安全测试
 */

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <string>
#include <vector>
#include <map>
#include <chrono>
#include <thread>
#include <atomic>
#include <sstream>
#include <algorithm>

// 模拟网络工具类
namespace {

/*!
 * \brief URL信息结构体
 */
struct UrlInfo {
    std::string protocol;
    std::string host;
    int port;
    std::string path;
    std::string query;
    std::string fragment;
    
    UrlInfo() : port(80) {}
};

/*!
 * \brief HTTP响应结构体
 */
struct HttpResponse {
    int status_code;
    std::map<std::string, std::string> headers;
    std::string body;
    
    HttpResponse() : status_code(0) {}
};

/*!
 * \brief 网络工具类
 * \details 提供常用的网络处理功能
 */
class NetworkUtils {
public:
    /*!
     * \brief 解析URL
     * \param url URL字符串
     * \return URL信息结构体
     */
    static UrlInfo parseUrl(const std::string& url) {
        UrlInfo info;
        
        if (url.empty()) return info;
        
        std::string remaining = url;
        
        // 解析协议
        size_t protocol_pos = remaining.find("://");
        if (protocol_pos != std::string::npos) {
            info.protocol = remaining.substr(0, protocol_pos);
            remaining = remaining.substr(protocol_pos + 3);
        }
        
        // 解析主机和端口
        size_t path_pos = remaining.find('/');
        std::string host_port = (path_pos != std::string::npos) ? 
                               remaining.substr(0, path_pos) : remaining;
        
        size_t port_pos = host_port.find(':');
        if (port_pos != std::string::npos) {
            info.host = host_port.substr(0, port_pos);
            std::string port_str = host_port.substr(port_pos + 1);
            info.port = std::stoi(port_str);
        } else {
            info.host = host_port;
            info.port = (info.protocol == "https") ? 443 : 80;
        }
        
        // 解析路径、查询和片段
        if (path_pos != std::string::npos) {
            remaining = remaining.substr(path_pos);
            
            size_t query_pos = remaining.find('?');
            size_t fragment_pos = remaining.find('#');
            
            if (query_pos != std::string::npos) {
                info.path = remaining.substr(0, query_pos);
                
                size_t query_end = (fragment_pos != std::string::npos) ? fragment_pos : remaining.length();
                info.query = remaining.substr(query_pos + 1, query_end - query_pos - 1);
                
                if (fragment_pos != std::string::npos) {
                    info.fragment = remaining.substr(fragment_pos + 1);
                }
            } else if (fragment_pos != std::string::npos) {
                info.path = remaining.substr(0, fragment_pos);
                info.fragment = remaining.substr(fragment_pos + 1);
            } else {
                info.path = remaining;
            }
        }
        
        return info;
    }
    
    /*!
     * \brief 构建URL
     * \param info URL信息结构体
     * \return URL字符串
     */
    static std::string buildUrl(const UrlInfo& info) {
        std::string url;
        
        if (!info.protocol.empty()) {
            url += info.protocol + "://";
        }
        
        url += info.host;
        
        if (info.port != 80 && info.port != 443) {
            url += ":" + std::to_string(info.port);
        }
        
        if (!info.path.empty()) {
            if (info.path[0] != '/') {
                url += "/";
            }
            url += info.path;
        }
        
        if (!info.query.empty()) {
            url += "?" + info.query;
        }
        
        if (!info.fragment.empty()) {
            url += "#" + info.fragment;
        }
        
        return url;
    }
    
    /*!
     * \brief URL编码
     * \param input 输入字符串
     * \return 编码后的字符串
     */
    static std::string urlEncode(const std::string& input) {
        std::string encoded;
        
        for (char c : input) {
            if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
                encoded += c;
            } else {
                encoded += '%';
                encoded += toHex((c >> 4) & 0xF);
                encoded += toHex(c & 0xF);
            }
        }
        
        return encoded;
    }
    
    /*!
     * \brief URL解码
     * \param input 编码的字符串
     * \return 解码后的字符串
     */
    static std::string urlDecode(const std::string& input) {
        std::string decoded;
        
        for (size_t i = 0; i < input.length(); ++i) {
            if (input[i] == '%' && i + 2 < input.length()) {
                int high = fromHex(input[i + 1]);
                int low = fromHex(input[i + 2]);
                if (high >= 0 && low >= 0) {
                    decoded += static_cast<char>((high << 4) | low);
                    i += 2;
                } else {
                    decoded += input[i];
                }
            } else if (input[i] == '+') {
                decoded += ' ';
            } else {
                decoded += input[i];
            }
        }
        
        return decoded;
    }
    
    /*!
     * \brief 解析查询字符串
     * \param query 查询字符串
     * \return 参数映射
     */
    static std::map<std::string, std::string> parseQuery(const std::string& query) {
        std::map<std::string, std::string> params;
        
        if (query.empty()) return params;
        
        std::stringstream ss(query);
        std::string pair;
        
        while (std::getline(ss, pair, '&')) {
            size_t eq_pos = pair.find('=');
            if (eq_pos != std::string::npos) {
                std::string key = urlDecode(pair.substr(0, eq_pos));
                std::string value = urlDecode(pair.substr(eq_pos + 1));
                params[key] = value;
            } else {
                params[urlDecode(pair)] = "";
            }
        }
        
        return params;
    }
    
    /*!
     * \brief 构建查询字符串
     * \param params 参数映射
     * \return 查询字符串
     */
    static std::string buildQuery(const std::map<std::string, std::string>& params) {
        std::string query;
        
        for (auto it = params.begin(); it != params.end(); ++it) {
            if (it != params.begin()) {
                query += "&";
            }
            query += urlEncode(it->first);
            if (!it->second.empty()) {
                query += "=" + urlEncode(it->second);
            }
        }
        
        return query;
    }
    
    /*!
     * \brief 模拟HTTP GET请求
     * \param url 请求URL
     * \return HTTP响应
     */
    static HttpResponse httpGet(const std::string& url) {
        HttpResponse response;
        
        // 模拟HTTP请求处理
        UrlInfo info = parseUrl(url);
        
        if (info.host.empty()) {
            response.status_code = 400;  // Bad Request
            response.body = "Invalid URL";
            return response;
        }
        
        // 模拟成功响应
        response.status_code = 200;
        response.headers["Content-Type"] = "application/json";
        response.headers["Content-Length"] = "100";
        response.headers["Server"] = "MockServer/1.0";
        response.body = "{\"status\":\"success\",\"data\":\"mock_data\"}";
        
        return response;
    }
    
    /*!
     * \brief 模拟HTTP POST请求
     * \param url 请求URL
     * \param data 请求数据
     * \return HTTP响应
     */
    static HttpResponse httpPost(const std::string& url, const std::string& data) {
        HttpResponse response;
        
        UrlInfo info = parseUrl(url);
        
        if (info.host.empty()) {
            response.status_code = 400;
            response.body = "Invalid URL";
            return response;
        }
        
        if (data.empty()) {
            response.status_code = 400;
            response.body = "No data provided";
            return response;
        }
        
        // 模拟成功响应
        response.status_code = 201;  // Created
        response.headers["Content-Type"] = "application/json";
        response.headers["Location"] = url + "/123";
        response.body = "{\"status\":\"created\",\"id\":123}";
        
        return response;
    }
    
    /*!
     * \brief 检查网络连接
     * \param host 主机地址
     * \param port 端口号
     * \return 是否连接成功
     */
    static bool checkConnection(const std::string& host, int port) {
        // 模拟连接检查
        if (host.empty() || port <= 0 || port > 65535) {
            return false;
        }
        
        // 模拟一些常见的主机连接状态
        if (host == "localhost" || host == "127.0.0.1") {
            return true;
        }
        
        if (host.find("invalid") != std::string::npos) {
            return false;
        }
        
        return true;  // 默认假设连接成功
    }
    
    /*!
     * \brief 获取本机IP地址
     * \return IP地址字符串
     */
    static std::string getLocalIP() {
        // 模拟获取本机IP
        return "192.168.1.100";
    }
    
    /*!
     * \brief 验证IP地址格式
     * \param ip IP地址字符串
     * \return 是否为有效IP地址
     */
    static bool isValidIP(const std::string& ip) {
        if (ip.empty()) return false;
        
        std::stringstream ss(ip);
        std::string segment;
        int count = 0;
        
        while (std::getline(ss, segment, '.')) {
            if (segment.empty()) return false;
            
            try {
                int num = std::stoi(segment);
                if (num < 0 || num > 255) return false;
            } catch (...) {
                return false;
            }
            
            count++;
        }
        
        return count == 4;
    }
    
    /*!
     * \brief 验证端口号
     * \param port 端口号
     * \return 是否为有效端口号
     */
    static bool isValidPort(int port) {
        return port > 0 && port <= 65535;
    }

private:
    static char toHex(int value) {
        return (value < 10) ? ('0' + value) : ('A' + value - 10);
    }
    
    static int fromHex(char c) {
        if (c >= '0' && c <= '9') return c - '0';
        if (c >= 'A' && c <= 'F') return c - 'A' + 10;
        if (c >= 'a' && c <= 'f') return c - 'a' + 10;
        return -1;
    }
};

} // anonymous namespace

/*!
 * \brief 网络工具类测试套件
 */
class NetworkUtilsTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 准备测试数据
        test_urls = {
            "http://www.example.com/path?param=value#section",
            "https://api.example.com:8080/v1/users",
            "ftp://files.example.com/download/file.txt",
            "http://localhost:3000/",
            "https://secure.example.com/login?redirect=/dashboard"
        };
    }
    
    void TearDown() override {
        // 测试后清理
    }
    
    std::vector<std::string> test_urls;
};

/*!
 * \brief 测试URL解析功能
 */
TEST_F(NetworkUtilsTest, TestUrlParsing) {
    // 测试完整URL解析
    std::string full_url = "https://www.example.com:8080/api/v1/users?page=1&limit=10#results";
    UrlInfo info = NetworkUtils::parseUrl(full_url);
    
    EXPECT_EQ(info.protocol, "https");
    EXPECT_EQ(info.host, "www.example.com");
    EXPECT_EQ(info.port, 8080);
    EXPECT_EQ(info.path, "/api/v1/users");
    EXPECT_EQ(info.query, "page=1&limit=10");
    EXPECT_EQ(info.fragment, "results");
    
    // 测试简单URL
    UrlInfo simple = NetworkUtils::parseUrl("http://example.com");
    EXPECT_EQ(simple.protocol, "http");
    EXPECT_EQ(simple.host, "example.com");
    EXPECT_EQ(simple.port, 80);
    EXPECT_TRUE(simple.path.empty());
    
    // 测试HTTPS默认端口
    UrlInfo https_info = NetworkUtils::parseUrl("https://secure.com/path");
    EXPECT_EQ(https_info.port, 443);
    
    // 测试空URL
    UrlInfo empty = NetworkUtils::parseUrl("");
    EXPECT_TRUE(empty.host.empty());
    
    std::cout << "URL解析功能测试通过" << std::endl;
}

/*!
 * \brief 测试URL构建功能
 */
TEST_F(NetworkUtilsTest, TestUrlBuilding) {
    // 构建完整URL
    UrlInfo info;
    info.protocol = "https";
    info.host = "api.example.com";
    info.port = 8080;
    info.path = "/v1/data";
    info.query = "format=json&limit=100";
    info.fragment = "section1";
    
    std::string built_url = NetworkUtils::buildUrl(info);
    EXPECT_EQ(built_url, "https://api.example.com:8080/v1/data?format=json&limit=100#section1");
    
    // 构建简单URL
    UrlInfo simple;
    simple.protocol = "http";
    simple.host = "example.com";
    simple.path = "/index.html";
    
    std::string simple_url = NetworkUtils::buildUrl(simple);
    EXPECT_EQ(simple_url, "http://example.com/index.html");
    
    // 测试往返转换
    for (const auto& original_url : test_urls) {
        UrlInfo parsed = NetworkUtils::parseUrl(original_url);
        std::string rebuilt = NetworkUtils::buildUrl(parsed);
        
        // 解析重建的URL应该得到相同的结果
        UrlInfo reparsed = NetworkUtils::parseUrl(rebuilt);
        EXPECT_EQ(parsed.protocol, reparsed.protocol);
        EXPECT_EQ(parsed.host, reparsed.host);
        EXPECT_EQ(parsed.path, reparsed.path);
    }
    
    std::cout << "URL构建功能测试通过" << std::endl;
}

/*!
 * \brief 测试URL编码解码
 */
TEST_F(NetworkUtilsTest, TestUrlEncoding) {
    // 测试基本编码
    std::string original = "Hello World!";
    std::string encoded = NetworkUtils::urlEncode(original);
    std::string decoded = NetworkUtils::urlDecode(encoded);
    
    EXPECT_NE(encoded, original);  // 应该被编码
    EXPECT_EQ(decoded, original);  // 解码后应该相同
    
    // 测试特殊字符编码
    std::string special = "name=张三&age=25";
    std::string special_encoded = NetworkUtils::urlEncode(special);
    std::string special_decoded = NetworkUtils::urlDecode(special_encoded);
    
    EXPECT_EQ(special_decoded, special);
    
    // 测试空字符串
    EXPECT_EQ(NetworkUtils::urlEncode(""), "");
    EXPECT_EQ(NetworkUtils::urlDecode(""), "");
    
    // 测试已编码的字符串
    std::string pre_encoded = "Hello%20World%21";
    std::string pre_decoded = NetworkUtils::urlDecode(pre_encoded);
    EXPECT_EQ(pre_decoded, "Hello World!");
    
    // 测试加号替换空格
    std::string plus_encoded = "Hello+World";
    std::string plus_decoded = NetworkUtils::urlDecode(plus_encoded);
    EXPECT_EQ(plus_decoded, "Hello World");
    
    std::cout << "URL编码解码测试通过" << std::endl;
}

/*!
 * \brief 测试查询字符串处理
 */
TEST_F(NetworkUtilsTest, TestQueryStringHandling) {
    // 测试查询字符串解析
    std::string query = "name=John&age=30&city=New%20York&active=true";
    auto params = NetworkUtils::parseQuery(query);
    
    EXPECT_EQ(params.size(), 4);
    EXPECT_EQ(params["name"], "John");
    EXPECT_EQ(params["age"], "30");
    EXPECT_EQ(params["city"], "New York");
    EXPECT_EQ(params["active"], "true");
    
    // 测试查询字符串构建
    std::map<std::string, std::string> build_params = {
        {"user", "admin"},
        {"password", "secret123"},
        {"remember", "true"}
    };
    
    std::string built_query = NetworkUtils::buildQuery(build_params);
    auto reparsed_params = NetworkUtils::parseQuery(built_query);
    
    EXPECT_EQ(reparsed_params.size(), build_params.size());
    for (const auto& pair : build_params) {
        EXPECT_EQ(reparsed_params[pair.first], pair.second);
    }
    
    // 测试空查询字符串
    auto empty_params = NetworkUtils::parseQuery("");
    EXPECT_TRUE(empty_params.empty());
    
    std::string empty_query = NetworkUtils::buildQuery({});
    EXPECT_TRUE(empty_query.empty());
    
    // 测试特殊情况
    auto special_params = NetworkUtils::parseQuery("key1=&key2=value2&key3");
    EXPECT_EQ(special_params["key1"], "");
    EXPECT_EQ(special_params["key2"], "value2");
    EXPECT_EQ(special_params["key3"], "");
    
    std::cout << "查询字符串处理测试通过" << std::endl;
}

/*!
 * \brief 测试HTTP请求模拟
 */
TEST_F(NetworkUtilsTest, TestHttpRequests) {
    // 测试GET请求
    std::string get_url = "http://api.example.com/users";
    HttpResponse get_response = NetworkUtils::httpGet(get_url);
    
    EXPECT_EQ(get_response.status_code, 200);
    EXPECT_FALSE(get_response.body.empty());
    EXPECT_TRUE(get_response.headers.find("Content-Type") != get_response.headers.end());
    
    // 测试POST请求
    std::string post_url = "http://api.example.com/users";
    std::string post_data = "{\"name\":\"John\",\"age\":30}";
    HttpResponse post_response = NetworkUtils::httpPost(post_url, post_data);
    
    EXPECT_EQ(post_response.status_code, 201);
    EXPECT_FALSE(post_response.body.empty());
    
    // 测试无效URL
    HttpResponse invalid_response = NetworkUtils::httpGet("");
    EXPECT_EQ(invalid_response.status_code, 400);
    
    // 测试POST无数据
    HttpResponse no_data_response = NetworkUtils::httpPost(get_url, "");
    EXPECT_EQ(no_data_response.status_code, 400);
    
    std::cout << "HTTP请求模拟测试通过" << std::endl;
}

/*!
 * \brief 测试网络连接检查
 */
TEST_F(NetworkUtilsTest, TestConnectionCheck) {
    // 测试本地连接
    EXPECT_TRUE(NetworkUtils::checkConnection("localhost", 80));
    EXPECT_TRUE(NetworkUtils::checkConnection("127.0.0.1", 443));
    
    // 测试无效连接
    EXPECT_FALSE(NetworkUtils::checkConnection("invalid.host.com", 80));
    EXPECT_FALSE(NetworkUtils::checkConnection("", 80));
    EXPECT_FALSE(NetworkUtils::checkConnection("example.com", 0));
    EXPECT_FALSE(NetworkUtils::checkConnection("example.com", 70000));
    
    // 测试有效主机
    EXPECT_TRUE(NetworkUtils::checkConnection("example.com", 80));
    EXPECT_TRUE(NetworkUtils::checkConnection("api.example.com", 443));
    
    std::cout << "网络连接检查测试通过" << std::endl;
}

/*!
 * \brief 测试IP地址验证
 */
TEST_F(NetworkUtilsTest, TestIPValidation) {
    // 测试有效IP地址
    EXPECT_TRUE(NetworkUtils::isValidIP("192.168.1.1"));
    EXPECT_TRUE(NetworkUtils::isValidIP("127.0.0.1"));
    EXPECT_TRUE(NetworkUtils::isValidIP("0.0.0.0"));
    EXPECT_TRUE(NetworkUtils::isValidIP("255.255.255.255"));
    
    // 测试无效IP地址
    EXPECT_FALSE(NetworkUtils::isValidIP(""));
    EXPECT_FALSE(NetworkUtils::isValidIP("192.168.1"));
    EXPECT_FALSE(NetworkUtils::isValidIP("192.168.1.256"));
    EXPECT_FALSE(NetworkUtils::isValidIP("192.168.1.1.1"));
    EXPECT_FALSE(NetworkUtils::isValidIP("abc.def.ghi.jkl"));
    EXPECT_FALSE(NetworkUtils::isValidIP("192.168.-1.1"));
    
    // 测试边界情况
    EXPECT_TRUE(NetworkUtils::isValidIP("0.0.0.0"));
    EXPECT_TRUE(NetworkUtils::isValidIP("255.255.255.255"));
    EXPECT_FALSE(NetworkUtils::isValidIP("256.0.0.0"));
    
    std::cout << "IP地址验证测试通过" << std::endl;
}

/*!
 * \brief 测试端口号验证
 */
TEST_F(NetworkUtilsTest, TestPortValidation) {
    // 测试有效端口号
    EXPECT_TRUE(NetworkUtils::isValidPort(1));
    EXPECT_TRUE(NetworkUtils::isValidPort(80));
    EXPECT_TRUE(NetworkUtils::isValidPort(443));
    EXPECT_TRUE(NetworkUtils::isValidPort(8080));
    EXPECT_TRUE(NetworkUtils::isValidPort(65535));
    
    // 测试无效端口号
    EXPECT_FALSE(NetworkUtils::isValidPort(0));
    EXPECT_FALSE(NetworkUtils::isValidPort(-1));
    EXPECT_FALSE(NetworkUtils::isValidPort(65536));
    EXPECT_FALSE(NetworkUtils::isValidPort(100000));
    
    std::cout << "端口号验证测试通过" << std::endl;
}

/*!
 * \brief 测试性能特性
 */
TEST_F(NetworkUtilsTest, TestPerformanceCharacteristics) {
    const int iterations = 1000;
    
    auto start_time = std::chrono::high_resolution_clock::now();
    
    // 测试URL解析性能
    for (int i = 0; i < iterations; ++i) {
        for (const auto& url : test_urls) {
            UrlInfo info = NetworkUtils::parseUrl(url);
            EXPECT_FALSE(info.host.empty());
        }
    }
    
    auto parse_time = std::chrono::high_resolution_clock::now();
    
    // 测试URL编码性能
    std::string test_string = "Hello World! 测试字符串 #$%^&*()";
    for (int i = 0; i < iterations; ++i) {
        std::string encoded = NetworkUtils::urlEncode(test_string);
        std::string decoded = NetworkUtils::urlDecode(encoded);
        EXPECT_EQ(decoded, test_string);
    }
    
    auto encode_time = std::chrono::high_resolution_clock::now();
    
    // 计算耗时
    auto parse_duration = std::chrono::duration_cast<std::chrono::milliseconds>(parse_time - start_time);
    auto encode_duration = std::chrono::duration_cast<std::chrono::milliseconds>(encode_time - parse_time);
    
    std::cout << "性能测试结果:" << std::endl;
    std::cout << "  " << (iterations * test_urls.size()) << " 次URL解析耗时: " << parse_duration.count() << "ms" << std::endl;
    std::cout << "  " << iterations << " 次URL编码解码耗时: " << encode_duration.count() << "ms" << std::endl;
    
    // 性能应该在合理范围内
    EXPECT_LT(parse_duration.count(), 1000);
    EXPECT_LT(encode_duration.count(), 1000);
}

/*!
 * \brief 测试多线程安全性
 */
TEST_F(NetworkUtilsTest, TestThreadSafety) {
    const int num_threads = 4;
    const int operations_per_thread = 100;
    std::atomic<int> success_count{0};
    
    std::vector<std::thread> threads;
    
    for (int t = 0; t < num_threads; ++t) {
        threads.emplace_back([&, t]() {
            for (int i = 0; i < operations_per_thread; ++i) {
                // 测试各种网络操作的线程安全性
                std::string test_url = "http://example" + std::to_string(t) + ".com/path" + std::to_string(i);
                
                UrlInfo info = NetworkUtils::parseUrl(test_url);
                std::string rebuilt = NetworkUtils::buildUrl(info);
                std::string encoded = NetworkUtils::urlEncode(test_url);
                std::string decoded = NetworkUtils::urlDecode(encoded);
                bool valid_ip = NetworkUtils::isValidIP("192.168.1." + std::to_string(i % 255));
                bool valid_port = NetworkUtils::isValidPort(8000 + i);
                
                if (!info.host.empty() && !rebuilt.empty() && 
                    decoded == test_url && valid_port) {
                    success_count++;
                }
            }
        });
    }
    
    for (auto& thread : threads) {
        thread.join();
    }
    
    EXPECT_EQ(success_count.load(), num_threads * operations_per_thread);
    std::cout << "多线程安全性测试通过，成功操作: " << success_count.load() << " 次" << std::endl;
}

/*!
 * \brief 测试完整的网络处理工作流程
 */
TEST_F(NetworkUtilsTest, TestCompleteNetworkWorkflow) {
    // 第一步：解析目标URL
    std::string target_url = "https://api.trading.com:8080/v1/market/data?symbol=AAPL&period=1d#latest";
    UrlInfo url_info = NetworkUtils::parseUrl(target_url);
    
    // 第二步：验证URL组件
    EXPECT_EQ(url_info.protocol, "https");
    EXPECT_EQ(url_info.host, "api.trading.com");
    EXPECT_EQ(url_info.port, 8080);
    EXPECT_EQ(url_info.path, "/v1/market/data");
    
    // 第三步：解析查询参数
    auto query_params = NetworkUtils::parseQuery(url_info.query);
    EXPECT_EQ(query_params["symbol"], "AAPL");
    EXPECT_EQ(query_params["period"], "1d");
    
    // 第四步：修改查询参数
    query_params["format"] = "json";
    query_params["limit"] = "100";
    std::string new_query = NetworkUtils::buildQuery(query_params);
    
    // 第五步：构建新的URL
    url_info.query = new_query;
    url_info.fragment = "";  // 移除片段
    std::string new_url = NetworkUtils::buildUrl(url_info);
    
    // 第六步：检查连接
    bool can_connect = NetworkUtils::checkConnection(url_info.host, url_info.port);
    
    // 第七步：模拟HTTP请求
    HttpResponse response = NetworkUtils::httpGet(new_url);
    
    // 第八步：处理响应
    bool request_successful = (response.status_code == 200);
    
    // 第九步：获取本机信息
    std::string local_ip = NetworkUtils::getLocalIP();
    bool valid_local_ip = NetworkUtils::isValidIP(local_ip);
    
    // 验证工作流程结果
    EXPECT_FALSE(new_url.empty());
    EXPECT_TRUE(can_connect);
    EXPECT_TRUE(request_successful);
    EXPECT_TRUE(valid_local_ip);
    EXPECT_TRUE(new_url.find("format=json") != std::string::npos);
    EXPECT_TRUE(new_url.find("limit=100") != std::string::npos);
    EXPECT_TRUE(new_url.find("#latest") == std::string::npos);  // 片段已移除
    
    // 输出工作流程结果
    std::cout << "完整网络处理工作流程测试结果:" << std::endl;
    std::cout << "  原始URL: " << target_url << std::endl;
    std::cout << "  新URL: " << new_url << std::endl;
    std::cout << "  主机: " << url_info.host << ":" << url_info.port << std::endl;
    std::cout << "  连接状态: " << (can_connect ? "可连接" : "无法连接") << std::endl;
    std::cout << "  请求状态: " << response.status_code << std::endl;
    std::cout << "  本机IP: " << local_ip << std::endl;
    std::cout << "  查询参数数量: " << query_params.size() << std::endl;
    
    std::cout << "完整网络处理工作流程测试通过" << std::endl;
} 